<?php

/**
 * RP System
 *
 * @version 1.0
 * @copyright 2004 Ron Planken
 */

class player {

	function get($player_id = -1, $class = 0, $active = -1, $user_id = 0, $latest = 0, $team_id = 0) {

		global $dbh;

		if ($player_id == -1) {
			$query ="SELECT  p.player_id
							,p.user_id
							,p.name
							,c.name as 'class'
							,p.class_id
							,c.small_picture as 'class_picture'
							,r.small_picture as 'role_picture'
							,p.race
							,p.level
							,p.talent1	
							,p.talent2	
							,p.talent3
							,p.date_created
							,p.date_removed
							,p.remover_id
							,p.removed
							,p.guild_id
							,p.team_id
							,t.team_image
							,t.team_name
							,t.team_order
							,p.class_role_id
							,c.color_code
							,cr.name as 'class_role_name'
							,r.name as 'role_name'
							,r.id as 'role_id'
							,cr.id as 'class_role_id'
					   FROM player p
					   JOIN team t ON t.team_id = p.team_id
					   JOIN class c ON c.class_id = p.class_id
				  LEFT JOIN class_role cr ON p.class_role_id = cr.id
				  LEFT JOIN role r ON cr.role_id = r.id
				  LEFT JOIN profile pr ON pr.player_id = p.player_id
					  WHERE removed = 0

							";
				if ($sort <> "") {
					$query .= " ORDER BY p.$sort ASC";
				} else {
					$query .= " ORDER by t.team_order DESC, p.class_id ASC";
				}
		} else {
			$query ="SELECT  p.player_id
							,p.user_id
							,p.name
							,c.name as 'class'
							,p.class_id
							,c.small_picture as 'class_picture'
							,r.small_picture as 'role_picture'
							,pr.race
							,pr.level
							,pr.talent1	
							,pr.talent2	
							,pr.talent3
							,p.date_created
							,p.date_removed
							,p.remover_id
							,p.removed
							,p.guild_id
							,p.team_id
							,t.team_image
							,t.team_name
							,t.team_order
							,p.class_role_id
							,c.color_code
							,cr.name as 'class_role_name'
							,r.name as 'role_name'
							,r.id as 'role_id'
							,cr.id as 'class_role_id'
					   FROM player p
					   JOIN team t ON t.team_id = p.team_id
					   JOIN class c ON c.class_id = p.class_id
				  LEFT JOIN class_role cr ON p.class_role_id = cr.id
				  LEFT JOIN role r ON cr.role_id = r.id
				  LEFT JOIN profile pr ON pr.player_id = p.player_id
					  WHERE removed = 0
							";


			if ($user_id > 0) {
				$query .= " AND p.user_id = $user_id";
			}
			if ($player_id > 0) {
				$query .= " AND p.player_id = $player_id";
			}
			if ($class > 0) {
				$query .= " AND p.class_id = $class_id";
			}
			if ($team > 0) {
				$query .= " AND p.team_id = $team_id";
			}
			if ($active <> -1) {
				$query .= " AND p.active = $active";
			}
			if ($latest == 1) {
				$query .= " AND order by date_created limit 1,1";
			}
		}

		$players = selectMultiQuery($query, $dbh);

		return $players;
	}

	function getOption($class = "", $rank = 0) {
		
		global $dbh;

		$query ='SELECT p.player_id
							,p.name
					   FROM player p
							';

		if ($class <> "") {

			$class = secure_string($class);

			$query .= "WHERE p.class = $class";

		}

		if ($rank > 0) {

			$rank = secure_string($rank);

			$query .= "JOIN team t on p.team_id = t.team_id
					  WHERE t.team_order > $rank";

		}
		
		$players = selectSingleTextQuery($query, $dbh);

		return $players;

	}

	function getId($name) {
		global $dbh;

		$query ="SELECT 	p.player_id
						   ,p.name
						   ,c.name as 'class'
						   ,c.small_picture as 'class_picture'
						   ,p.user_id
					   FROM player p
					   JOIN class c ON c.class_id = p.class_id
					  WHERE p.name = '$name'
					  	AND removed = 0";

		$player = selectMultiQuery($query, $dbh);

		return $player;

	}

	function isOwner($player_id) {
		global $dbh;

		$player_id = secure_int($player_id);

		$query ="SELECT 	p.user_id
					   FROM player p
					  WHERE p.player_id = $player_id";

		$user_id = selectQuery($query, $dbh);

        if ($user_id == $_SESSION['uid'])
        {
            return true;
        } else {
            return false;
        }
	}

	function save($player_id, $user_id, $name, $class_id, $race, $level, $team_id, $remover_id = 0, $date_removed = 0, $removed = 0, $role_id = 0) {

		global $dbh;

		$player_id = secure_int($player_id);
		$user_id = secure_int($user_id);
		$name = secure_string( $name);
		$class = secure_int( $class);
		$race = secure_string($race );
		$level = secure_int( $level);
		$role_id = secure_int( $role_id);
		$team_id = secure_int($team_id);
		
		
		if ($player_id == 0) {
			$query ="INSERT INTO player
						   ( user_id
							,name
							,class_id
							,race
							,`level` 
							,date_created
							,date_removed
							,remover_id
							,removed
							,guild_id
							,team_id
							,class_role_id)
					VALUES ( $user_id
							,$name
							,$class_id
							,$race
							,$level
							,Now()
							,0
							,0
							,0
							,0
							,1
							,$role_id)
							";
			$status = InsertQuery($query, $dbh);

			$status = mysql_insert_id();

		} else {
			$query ="UPDATE player
						SET  name = $name
							,class_id = $class_id
							,race = $race
							,level = $level
							,date_removed = $date_removed
							,remover_id = $remover_id
							,removed = $removed
							,guild_id = 0
							,team_id = $team_id
							";
			if ($role_id > 0)
				$query .= " ,class_role_id = $role_id";
				
			$query .= " WHERE player_id = $player_id";

			
			echo $query;
			
			$status = UpdateQuery($query, $dbh);
		}

		return $status;

	}

	function remove($player_id = 0) {

		global $dbh;

		$remover_id = $_SESSION['uid'];

		$query ="UPDATE player
						SET removed = 1
						   ,date_removed = Now()
						   ,remover_id = $remover_id
					  WHERE player_id = $player_id
							";
		$status = UpdateQuery($query, $dbh);

		return $status;
	}

	function activate($player_id = 0, $user_id = 0) {

		global $dbh;

		$query ="UPDATE player
						SET active = 1
					  WHERE player_id = $player_id
					  	AND user_id = $user_id
							";
		$status = UpdateQuery($query, $dbh);

		return $status;
	}



	function deactivate($user_id = 0) {

		global $dbh;

		$query ="UPDATE player
						SET active = 0
					  WHERE user_id = $user_id
							";
		$status = UpdateQuery($query, $dbh);

		return $status;
	}

	function count($user_id = 0, $class = '', $team = 0) {

		global $dbh;

		if ($class <> '') {
			$query =	"SELECT c.name as 'class', Count(*) as total
					   	   FROM player p
					       JOIN class c ON c.class_id = p.class_id
						  WHERE team_id > 2
						    AND removed = 0
							";

			$query .= "GROUP BY p.class_id";

			$count = selectMultiTextQuery($query, $dbh);

		}	else {

			$query ="SELECT Count(*) as total
					   FROM player
					  WHERE team_id > 2
					    AND removed = 0
							";
			if ($user_id > 0) {

				$query .= "AND user_id = $user_id";

			}

			if ($team <> '') {

				$query .= "GROUP BY team_id";

			}

			$count = selectMultiQuery($query, $dbh);

		}

		return $count;
	}

}
?>